package com.teach;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * <pre>
 * - UDP发送端
 * Author : J.L.Zhou
 * E-Mail : 2233875735@qq.com
 * Tel : 151 1104 7708
 * Date : 2021-4-29 15:10:34
 * Version : 1.0
 * Copyright 2021 jlzhou.top Inc. All rights reserved.
 * Warning: this content is only for internal circulation of the company.
 *          It is forbidden to divulge it or use it for other commercial purposes.
 * </pre>
 */

public class UDPSender {

	public static void main(String[] args) {
		try{
			int port = 18000;
			//1.创建对象
	        //构造数据报套接字并将其绑定到本地主机上任何可用的端口。
	        DatagramSocket socket = new DatagramSocket();
	        //2.打包
	        byte[] arr = "这是要发送的文字".getBytes("UTF-8");
	        //四个参数: 包的数据  包的长度  主机对象  端口号                      
	        DatagramPacket packet = new DatagramPacket
	                (arr, arr.length,InetAddress.getByName("192.168.31.10") , port);
	        
	        //3.发送
	        System.out.println("发送数据包");
	        socket.send(packet);
	        Thread.sleep(5500);
	        System.out.println("发送数据包");
	        socket.send(packet);
	        
	        //4.关闭资源
	        socket.close();
		}catch(Exception ex){ 
			ex.printStackTrace();
		}
	}
}